package com.his.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.DrawbackMedicinal;
import com.his.entity.Recipel;
import com.his.mapper.DrawbackMedicinalMapper;
import com.his.service.DrawbackMedicinalService;
import com.his.service.OrderService;
import com.his.service.RecipelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (DrawbackMedicinal)表服务实现类
 *
 * @author makejava
 * @since 2020-10-23 18:09:36
 */
@Service("drawbackMedicinalService")
public class DrawbackMedicinalServiceImpl implements DrawbackMedicinalService {
    @Resource
    private DrawbackMedicinalMapper drawbackMedicinalMapper;

    @Autowired
    private RecipelService recipelService;

    @Resource
    private OrderService orderService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public DrawbackMedicinal queryById(Integer id) {
        return this.drawbackMedicinalMapper.queryById(id);
    }

    /**
     * 查询所有通过实体类属性
     *
     * @param drawbackMedicinal 实例对象
     * @return 查询的结果
     */
    @Override
    public List<DrawbackMedicinal> queryAll(DrawbackMedicinal drawbackMedicinal) {
        return this.drawbackMedicinalMapper.queryAll(drawbackMedicinal);
    }

    @Override
    public PageInfo<DrawbackMedicinal> queryAll(PageParam<DrawbackMedicinal> pageParam) {
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            return new PageInfo<DrawbackMedicinal>(drawbackMedicinalMapper.superSearch(pageParam.getSuperSearchKeyWord()));
        }
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        //排序
        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        return new PageInfo<DrawbackMedicinal>(drawbackMedicinalMapper.queryAllByModel(pageParam.getModel()));
    }

    /**
     * 新增数据
     *
     * @param drawbackMedicinal 实例对象
     * @return 实例对象
     */
    @Override
    public DrawbackMedicinal insert(DrawbackMedicinal drawbackMedicinal) {
        drawbackMedicinal.setCreateTime(new Date());
        //判断类别名称是否重复
        Integer queryByName = drawbackMedicinalMapper.queryByOrderNo(drawbackMedicinal.getOrderNo());
        if (queryByName > 0) {
            return null;
        }
        //没重复则返回添加的实体
        this.drawbackMedicinalMapper.insert(drawbackMedicinal);
        return drawbackMedicinal;
    }

    /**
     * 修改数据
     *
     * @param drawbackMedicinal 实例对象
     * @return 实例对象
     */
    @Override
    public DrawbackMedicinal update(DrawbackMedicinal drawbackMedicinal) {
        //判断类别名称是否重复
        Integer queryByName = drawbackMedicinalMapper.queryByOrderNo(drawbackMedicinal.getOrderNo());
        if (queryByName > 0) {
            return null;
        }
        //没重复则返回添加的实体
        this.drawbackMedicinalMapper.update(drawbackMedicinal);
        return drawbackMedicinal;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.drawbackMedicinalMapper.deleteById(id) > 0;
    }

    //逻辑删除
    @Override
    public boolean deleteByIdLogic(Integer id) {
        return this.drawbackMedicinalMapper.deleteByIdLogic(id) > 0;
    }

    //驳回退药
    @Override
    public Rsp handelmedicinalFuse(DrawbackMedicinal drawbackMedicinal) {
        //处方状态改为4
        System.out.println("=====================处方id" + drawbackMedicinal.getRecipelId());
        //根据退药单id查找退药单对象
        DrawbackMedicinal queryById = drawbackMedicinalMapper.queryById(drawbackMedicinal.getId());
        //获取处方id
        Rsp rsp = recipelService.queryById(queryById.getRecipelId());
        Recipel rec = (Recipel) rsp.getContent();
        rec.setStatus("4");
        //处方存入数据库
        recipelService.update(rec);
        drawbackMedicinal.setStatus(10);
        return drawbackMedicinalMapper.update(drawbackMedicinal) > 0 ? Rsp.success(drawbackMedicinal) : Rsp.fail().msg("操作失败");
    }

    //同意退药
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Rsp handelmedicinalSure(DrawbackMedicinal drawbackMedicinal) {

        //获取处方ID
        Integer recipelId = drawbackMedicinal.getRecipelId();

        //通过处方id找到订单id
        Rsp rsp = recipelService.queryById(recipelId);
        Recipel recipel = (Recipel) rsp.getContent();
        System.out.println("=======================rsp" + recipel.getOrderId());

        //通过订单id逻辑删除订单
        orderService.deleteByIdLogic(recipel.getOrderId());
        //通过处方ID逻辑删除该处方
        recipelService.deleteByIdLogic(recipelId);
        //修改退药单状态码为1
        drawbackMedicinal.setStatus(1);
        return drawbackMedicinalMapper.update(drawbackMedicinal) > 0 ? Rsp.success(drawbackMedicinal) : Rsp.fail().msg("操作失败");
    }

}
